package com._520it.wms.web.controller;

import com._520it.wms.domain.Role;
import com._520it.wms.query.QueryObject;
import com._520it.wms.service.IPermissionService;
import com._520it.wms.service.IRoleService;
import com._520it.wms.service.ISystemMenuService;
import com._520it.wms.util.JSONResult;
import com._520it.wms.util.RequiredPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by HASEE on 2017/11/14.
 */
@Controller
@RequestMapping("/role")
public class RoleContorller {
    @Autowired
    private IRoleService roleService;
    @Autowired
    private IPermissionService permissionService;
    @Autowired
    private ISystemMenuService systemMenuService;

    @RequestMapping("/list")
    @RequiredPermission("角色列表")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo) {
        model.addAttribute("pageResult", roleService.query(qo));
        return "/role/list";
    }

    @RequestMapping("/input")
    @RequiredPermission("角色编辑")
    public String input(Long id, Model model) {
        model.addAttribute("permissions", permissionService.listAll());
        //查询所有的子菜单 不显示系统,业务,报表模块
        model.addAttribute("menus",systemMenuService.listChildMenus());
        if (id != null) {
            Role role = roleService.get(id);
            model.addAttribute("role", role);
        }
        return "/role/input";
    }

    @RequestMapping("delete")
    @RequiredPermission("角色删除")
    @ResponseBody
    public JSONResult delete(Long id) {
        if (id != null) {
            roleService.delete(id);
        }
        return new JSONResult();
    }

    @RequestMapping("/saveOrUpdate")
    @RequiredPermission("角色新增/更新")
    @ResponseBody
    public JSONResult saveOrUpdate(Role role, Long[] permissionIds,Long[] menuIds) {
        roleService.saveOrUpdate(role, permissionIds,menuIds);
        return new JSONResult();

    }


}
